package com.ysy.logistics.service.oam.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.oam.LoginLog;
import com.ysy.logistics.entities.oam.OperationLog;
import com.ysy.logistics.entities.system.SysUser;
import com.ysy.logistics.mapper.oam.OperationLogMapper;
import com.ysy.logistics.mapper.system.SysUserMapper;
import com.ysy.logistics.service.oam.OperationLogService;
import com.ysy.logistics.util.PageInfo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/21 11:00
 **/
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    @Resource
    private OperationLogMapper operationLogMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public void add(OperationLog operationLog) {
        //是否存在角色ID
        String userId = operationLog.getUserId();
        if (StringUtils.isNotEmpty(userId)){
            //添加用户名
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.select(SysUser::getUsername)
                    .eq(SysUser::getId,userId);
            SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);
            if (sysUser != null){
                operationLog.setUserName(sysUser.getUsername());
            }
        }
        //保存
        operationLogMapper.insert(operationLog);
    }

    @Override
    public Page<OperationLog> listByPage(PageInfo<OperationLog> pageInfo) {
        //获取参数
        log.debug("分页查询登录日志："+pageInfo);
        LocalDate beginTime = pageInfo.getBeginTime();
        LocalDate endTime = pageInfo.getEndTime();
        if (beginTime != null && endTime != null){
            LocalDate temp = beginTime;
            endTime = beginTime.isAfter(endTime)?beginTime:endTime;
            beginTime = temp;
        }
        LambdaQueryWrapper<OperationLog> lqw = new LambdaQueryWrapper();
        if (beginTime!=null){
            lqw.ge(OperationLog::getCreateTime,beginTime.atStartOfDay());
        }
        if (endTime!=null){
            //+1天
            endTime.plusDays(1);
            lqw.le(OperationLog::getCreateTime,endTime.atStartOfDay());
        }
        OperationLog criteria = pageInfo.getCriteria();
        Optional.ofNullable(criteria).ifPresent(c -> {
            Optional.ofNullable(c.getLogType()).ifPresent(logType -> lqw.like(OperationLog::getLogType,logType));
            Optional.ofNullable(c.getLogName()).ifPresent(name -> lqw.like(OperationLog::getLogName,name));
        });
        lqw.orderByDesc(OperationLog::getCreateTime);

        return operationLogMapper.selectPage(pageInfo.getPage(), lqw);
    }
}
